package com.freight.tws.controller;

import java.util.List;
import java.io.IOException;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import lombok.RequiredArgsConstructor;
import com.freight.common.security.annotation.PreAuthorize;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.freight.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.freight.common.log.annotation.Log;
import com.freight.common.log.enums.BusinessType;
import com.freight.common.core.web.controller.BaseController;
import com.freight.common.core.web.domain.AjaxResult;
import com.freight.common.core.utils.poi.ExcelUtil;
import com.freight.common.core.web.page.Query;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.freight.common.core.web.page.Condition;
import com.freight.tws.domain.DriverBank;
import com.freight.tws.service.IDriverBankService;
import com.freight.common.core.web.page.TableDataInfo;

/**
 * 司机银行卡信息Controller
 * 
 * @author ruoyi
 * @date 2023-03-14
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/driverBank")
public class DriverBankController extends BaseController
{
    private final IDriverBankService iDriverBankService;

    /**
     * 查询司机银行卡信息列表
     */
    @PreAuthorize(hasPermi="tws:driverBank:list")
    @GetMapping("/list")
    public TableDataInfo list(Query query, DriverBank driverBank)
    {
        LambdaQueryWrapper<DriverBank> lqw = new LambdaQueryWrapper<DriverBank>();
        if (driverBank.getDriverId() != null) {
            lqw.eq(DriverBank::getDriverId, driverBank.getDriverId());
        }
        if (StringUtils.isNotBlank(driverBank.getCardType())) {
            lqw.eq(DriverBank::getCardType, driverBank.getCardType());
        }
        if (StringUtils.isNotBlank(driverBank.getCardNumber())) {
            lqw.eq(DriverBank::getCardNumber, driverBank.getCardNumber());
        }
        if (StringUtils.isNotBlank(driverBank.getBankName())) {
            lqw.like(DriverBank::getBankName ,driverBank.getBankName());
        }
        if (StringUtils.isNotBlank(driverBank.getValidToDate())) {
            lqw.eq(DriverBank::getValidToDate, driverBank.getValidToDate());
        }
        if (StringUtils.isNotBlank(driverBank.getIsDefault())) {
            lqw.eq(DriverBank::getIsDefault, driverBank.getIsDefault());
        }
        if (StringUtils.isNotBlank(driverBank.getCardImg())) {
            lqw.eq(DriverBank::getCardImg, driverBank.getCardImg());
        }
        lqw.orderByDesc(DriverBank::getCreateTime);
        IPage<DriverBank> list = iDriverBankService.page(Condition.getPage(query), lqw);
        return getDataTable(list);
    }

    /**
     * 导出司机银行卡信息列表
     */
    @PreAuthorize(hasPermi = "tws:driverBank:export")
    @Log(title = "司机银行卡信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DriverBank driverBank) throws IOException
    {
        LambdaQueryWrapper<DriverBank> lqw = new LambdaQueryWrapper<DriverBank>(driverBank);
        List<DriverBank> list = iDriverBankService.list(lqw);
        ExcelUtil<DriverBank> util = new ExcelUtil<DriverBank>(DriverBank. class);
        util.exportExcel(response, list, "driverBank");
    }

    /**
     * 获取司机银行卡信息详细信息
     */
    @PreAuthorize(hasPermi="tws:driverBank:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iDriverBankService.getById(id));
    }

    /**
     * 新增司机银行卡信息
     */
    @PreAuthorize(hasPermi="tws:driverBank:add")
    @Log(title = "司机银行卡信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DriverBank driverBank) {
        return toAjax(iDriverBankService.save(driverBank) ? 1 : 0);
    }

    /**
     * 修改司机银行卡信息
     */
    @PreAuthorize(hasPermi="tws:driverBank:edit")
    @Log(title = "司机银行卡信息" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DriverBank driverBank) {
        return toAjax(iDriverBankService.updateById(driverBank) ? 1 : 0);
    }

    /**
     * 删除司机银行卡信息
     */
    @PreAuthorize(hasPermi="tws:driverBank:remove")
    @Log(title = "司机银行卡信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iDriverBankService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

}
